package com.bytecodezz.app

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.bytecodezz.app.photoshop.loader.core.PhotoshopLoader
import com.bytecodezz.app.photoshop.loader.listener.BitmapLoaderListener
import com.bytecodezz.photoshop.compressor.CompressorTools
import com.bytecodezz.photoshop.compressor.OnCompressorListener
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import java.io.File


class MainActivity : AppCompatActivity() {
    var imageView: ImageView? = null
    var bitmap: Bitmap? = null

    var url11: String ="/storage/emulated/0/Android/data/com.bytecodezz.app/files/Pictures/62555.jpg"
    var url1: String =
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2F1812.img.pp.sohu.com.cn%2Fimages%2Fblog%2F2009%2F11%2F18%2F18%2F8%2F125b6560a6ag214.jpg&refer=http%3A%2F%2F1812.img.pp.sohu.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625392829&t=601f8efafcb41b46ad95c6f270514ee1"
    var url2: String =
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2F2c.zol-img.com.cn%2Fproduct%2F124_500x2000%2F748%2FceZOdKgDAFsq2.jpg&refer=http%3A%2F%2F2c.zol-img.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625392829&t=8ba9905d7a29d671c4ea17f4fb7182de"
    var url3: String =
        "https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/6a63f6246b600c33efad36eb1e4c510fd9f9a139.jpg"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        imageView = findViewById(R.id.imageview)
//        XXPermissions.setScopedStorage(true)
    }

    fun loaderA(view: View) {
        PhotoshopLoader.with(this)
            .load(url11)
            .into(imageView!!)
    }

    fun loaderB(view: View) {
        PhotoshopLoader.with(this)
            .load(url2)
            .asBitmap()
            .addListener(object : BitmapLoaderListener {
                override fun onLoadFailed(e: Exception?, model: Any?): Boolean {
                    return false
                }

                override fun onResourceReady(model: Any?, resource: Bitmap?): Boolean {
                    imageView!!.setImageBitmap(resource!!)
                    return false
                }

            })
            .into()
    }

    fun loaderCompressor(view: View) {
        PhotoshopLoader.with(this)
            .load(url3)
            .asBitmap()
            .addListener(object : BitmapLoaderListener {
                override fun onLoadFailed(e: Exception?, model: Any?): Boolean {
                    return false
                }

                override fun onResourceReady(model: Any?, resource: Bitmap?): Boolean {
                    bitmap = resource
//                    imageView!!.setImageBitmap(bitmap!!)
                    testCompressor(bitmap!!)
                    return false
                }

            })
            .into()
    }

    fun loaderC(view: View) {
        PhotoshopLoader.with(this)
            .load(R.mipmap.test)
            .into(imageView!!)

    }

    fun testCompressor(rawBitmap: Bitmap?) {

        val filepath: String =
            getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!.absolutePath + File.separator.toString() + "hand.jpg"


        XXPermissions.with(this) // 不适配 Android 11 可以这样写
            //.permission(Permission.Group.STORAGE)
            // 适配 Android 11 需要这样写，这里无需再写 Permission.Group.STORAGE
            .permission(Permission.MANAGE_EXTERNAL_STORAGE)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: List<String>, all: Boolean) {
                    CompressorTools().compressBitmap(
                        rawBitmap!!,
                        30,
                        filepath,
                        openChildThread = false,
                        listener = object : OnCompressorListener {
                            override fun onCompressorResult(
                                rawBitmap: Bitmap?,
                                outputPath: String?
                            ) {
                                imageView!!.setImageBitmap(rawBitmap!!)
                                Log.d("TestCompressor", "SSS")
                            }

                        })
                }

                override fun onDenied(permissions: List<String>, never: Boolean) {

                }
            })
    }

    fun loaderCompressorResult(view: View) {

        val filepath: String =
            getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!.absolutePath + File.separator.toString() + "hand.jpg"

        PhotoshopLoader.with(this)
            .load(filepath)
            .into(imageView!!)
    }

    fun toCompressor(view: View) {
        startActivity(Intent(this, PhotoCompressorActivity::class.java))
    }
}